<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Requests\Dashboard\SkillIndexRequest;
use App\Http\Requests\Dashboard\SkillStoreRequest;

use App\Http\Resources\Dashboard\SkillAutoCompleteResource;
use App\Http\Resources\Dashboard\SkillCollection;
use App\Http\Resources\Dashboard\SkillResource;
use App\Models\CareerSkill;
use App\Models\Skill;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SkillController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param SkillIndexRequest $request
     * @param Skill $skill
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(SkillIndexRequest $request, Skill $skill)
    {
        if ($request->has('title')) {
            $skill = $skill->like('title', $request->title);
        }
        if ($request->has('description')) {
            $skill = $skill->like('description', $request->description);
        }
        if ($request->has('trashed')) {
            $skill = $skill->trashedType($request->trashed);
        }
        $skill = $skill->withCount(['careers', 'courses']);
        return SkillResource::collection($skill->paginate($request->get('per_page', 15)));
    }

    public function autoComplete(Request $request, Skill $skill)
    {

        if ($request->filled('keyword')) {
            $skill = $skill->like('title', $request->keyword)->orLike('description', $request->keyword);
        }
//        if ($request->filled('career_id')) {
//            $skill = $skill->whereNotIn('id', function ($query) use ($request) {
//                return $query->select('id')->from('career_skill')->where('career_id', $request->career_id);
//            });
//        }
        $skill = $skill->limit($request->get('limit', 5));
        return SkillAutoCompleteResource::collection($skill->get());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \App\Http\Requests\Dashboard\SkillStoreRequest $request
     * @return \App\Http\Resources\Dashboard\SkillResource
     */
    public function store(SkillStoreRequest $request)
    {
        $skill = new Skill($request->only(['title', 'description']));
        $skill->save();

        return new SkillResource($skill);
    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\Skill $skill
     * @return \App\Http\Resources\Dashboard\SkillResource
     */
    public function show(Skill $skill)
    {
        return new SkillResource($skill);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\Skill $skill
     * @return \App\Http\Resources\Dashboard\SkillResource
     */
    public function update(Request $request, Skill $skill)
    {
        $skill->fill($request->only(['title', 'description']));
        $skill->save();

        return new SkillResource($skill);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param \App\Models\Skill $skill
     * @return \Illuminate\Http\Response
     */
    public function destroy(Skill $skill)
    {
        //
    }
}
